//
//  MXAddSetWifiController.m
//  HNBaseProject
//
//  Created by xujie on 2024/6/3.
//

#import "MXAddSetWifiController.h"
#import "MxPublicNav.h"
#import "MXAddSetContactController.h"
#import "DCWifiTool.h"
#import "DCAlterView.h"

@interface MXAddSetWifiController () <MxNavigationViewDelegate>
@property (nonatomic, strong) MxPublicNav *nav;
@property (nonatomic, strong) UILabel *selectLab;
@property (nonatomic, strong) UITextField *wifiNameLab;
@property (nonatomic, strong) UILabel *nextLab;
@property (nonatomic,strong) UITextField * passField;
@property (nonatomic, copy) NSString *passWord;
@end

@implementation MXAddSetWifiController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.passWord = @"";
    
    [self createUI];
}

- (void)createUI {
    
    self.nav = [[MxPublicNav alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kNavBarHeight)];
    self.nav.delegate = self;
    [self.view addSubview:self.nav];
    
    UILabel *addLab = [[UILabel alloc] initWithFrame:CGRectMake(24, CGRectGetMaxY(self.nav.frame) + kScalHeight(66), 200, 30)];
    [self.view addSubview:addLab];
    addLab.text = @"Wi-Fi 配置";
    addLab.textColor = [UIColor colorWithHexString:@"#221815"];
    addLab.font = PFRFontMedium(30);
    
    
    UIView *topBg = [[UIView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(addLab.frame) + 66, kScreenW-30, 49)];
    topBg.backgroundColor = [UIColor colorWithHexString:@"#F6F6F6"];
    [self.view addSubview:topBg];
    topBg.layer.cornerRadius = 15;
    topBg.layer.masksToBounds = YES;
    
    UIImageView *wifiIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"add_wifi_icon"]];
    [topBg addSubview:wifiIcon];
    [wifiIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(11);
        make.centerY.mas_equalTo(topBg);
        make.height.width.mas_equalTo(16);
    }];
    
    
    
    self.wifiNameLab = [[UITextField alloc] init];
    self.wifiNameLab.textColor = [UIColor colorWithHexString:@"#221815"];
    self.wifiNameLab.font = PFRFontMedium(14);
    self.wifiNameLab.placeholder = @"请输入WiFi账号";
    self.wifiNameLab.text = @"haoniu-dingding";
    [topBg addSubview:self.wifiNameLab];
    [self.wifiNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(40);
        make.right.offset(-80);
        make.top.bottom.offset(0);
//        make.centerY.mas_equalTo(topBg);
    }];
    
    
    MJWeakSelf;
    self.selectLab = [[UILabel alloc] init];
    [topBg addSubview:self.selectLab];
    self.selectLab.text = @"选择Wi-Fi";
    self.selectLab.userInteractionEnabled = YES;
    self.selectLab.font =  PFRFontMedium(12);
    self.selectLab.textColor = [UIColor colorWithHexString:@"#3278F6"];
    self.selectLab.backgroundColor = [UIColor dc_colorWithHexString:@"#3278F6" alpha:0.1];
    self.selectLab.layer.cornerRadius = 14;
    self.selectLab.layer.masksToBounds = YES;
    self.selectLab.textAlignment = NSTextAlignmentCenter;
    [self.selectLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(topBg);
        make.right.offset(-12);
        make.height.mas_equalTo(28);
        make.width.mas_equalTo(72);
    }];
    [self.selectLab addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakSelf selectWifi];
    }]];
    
    UIView *bottomBg = [[UIView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(topBg.frame)+28, kScreenW-30, 49)];
    bottomBg.backgroundColor = [UIColor colorWithHexString:@"#F6F6F6"];
    [self.view addSubview:bottomBg];
    bottomBg.layer.cornerRadius = 15;
    bottomBg.layer.masksToBounds = YES;
    
    UIImageView *lockIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"add_lock_icon"]];
    [bottomBg addSubview:lockIcon];
    [lockIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(11);
        make.centerY.mas_equalTo(bottomBg);
        make.height.width.mas_equalTo(16);
    }];
    
    UITextField *passField = [[UITextField alloc] init];
    passField.textColor = [UIColor colorWithHexString:@"#221815"];
    passField.font = PFRFontMedium(14);
    passField.text = @"gmftyphc";
    passField.placeholder = @"请输入密码";
    [bottomBg addSubview:passField];
    [passField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(40);
        make.right.offset(-10);
        make.top.bottom.offset(0);
    }];
    self.passField = passField;
    
    self.nextLab = [[UILabel alloc] init];
    [self.view addSubview:self.nextLab];
    self.nextLab.backgroundColor = [UIColor colorWithHexString:@"#3278F6"];
    self.nextLab.userInteractionEnabled = YES;
    self.nextLab.textColor = [UIColor whiteColor];
    self.nextLab.text = @"下一步";
    self.nextLab.font = PFRFontMedium(16);
    self.nextLab.textAlignment = NSTextAlignmentCenter;
    self.nextLab.layer.cornerRadius = 24;
    self.nextLab.layer.masksToBounds = YES;
    [self.nextLab addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakSelf nextStep];
    }]];
    
    [self.nextLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(14);
        make.right.offset(-14);
        make.height.mas_equalTo(48);
        make.bottom.offset(-50);
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(WillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
}

-(void)WillEnterForeground{
    if(![[DCWifiTool shareClient] hasLocationAuth]){
        DCAlterView * alterView = [[DCAlterView alloc] initWithTitle:@"系统提示" content:@"检测网络环境需要开启定位权限，请先开启定位权限"];
        [alterView addActionWithTitle:@"去开启" color:[UIColor colorWithHexString:@"#3378F8"] type:DCAlterTypeDone halderBlock:^(UIButton *button) {
            NSURL *appSettings = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            [[UIApplication sharedApplication] openURL:appSettings options:@{} completionHandler:^(BOOL success) {
            }];
        }];
        
        [DC_KeyWindow addSubview:alterView];
    }else{
        if(self.wifiNameLab.text.length == 0){
            NSString *wifiName = [[DCWifiTool shareClient] dc_connectWifiName];
            NSLog(@"wifiInfo%@",wifiName);
            if(wifiName && wifiName.length > 0){
                self.wifiNameLab.text = wifiName;
            }
        }
    }
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [self WillEnterForeground];
}

- (void)selectWifi {
    NSString *wifiName = [[DCWifiTool shareClient] dc_connectWifiName];
    NSLog(@"wifiInfo%@",wifiName);
    if(wifiName && wifiName.length > 0){
        self.wifiNameLab.text = wifiName;
    }
}

- (void)nextStep {
    if(self.wifiNameLab.text.length == 0){
        [SVProgressHUD showInfoWithStatus:@"请输入WiFi账号"];
        return;
    }
    if(self.passField.text.length == 0){
        [SVProgressHUD showInfoWithStatus:@"请输入WiFi密码"];
        return;
    }
    if(![[DCWifiTool shareClient] hasLocationAuth]){
        DCAlterView * alterView = [[DCAlterView alloc] initWithTitle:@"系统提示" content:@"连接喂食器需要开启定位权限，请先开启定位权限"];
        [alterView addActionWithTitle:@"去开启" color:[UIColor colorWithHexString:@"#3378F8"] type:DCAlterTypeDone halderBlock:^(UIButton *button) {
            NSURL *appSettings = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            [[UIApplication sharedApplication] openURL:appSettings options:@{} completionHandler:^(BOOL success) {
            }];
        }];
        
        [DC_KeyWindow addSubview:alterView];
        return;
    }
    MXAddSetContactController *contactVc = [[MXAddSetContactController alloc] init];
    contactVc.wifiName = self.wifiNameLab.text;
    contactVc.wifiPwd = self.passField.text;
    contactVc.categoryId = self.categoryId;
    contactVc.familyId = self.familyId;
    [self.navigationController pushViewController:contactVc animated:YES];
}

- (void)leftTap {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self dc_navBarHidden:YES];
}

@end
